<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Function的使用 | JavaScript高级讲义</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../Sources/参数代码太长的问题.html" />
    
    
    <link rel="prev" href="../Sources/function.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="1.10.2.1"
        data-chapter-title="Function的使用"
        data-filepath="Sources/function的使用.md"
        data-basepath=".."
        data-revision="Tue Oct 18 2016 23:20:06 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" >
            
            <span><b>1.</b> 面向对象</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="Sources/面向对象概念介绍.html">
            
                
                    <a href="../Sources/面向对象概念介绍.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        面向对象概念介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="Sources/面向对象编程举例.html">
            
                
                    <a href="../Sources/面向对象编程举例.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        面向对象编程举例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Sources/构造函数.html">
            
                
                    <a href="../Sources/构造函数.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        构造函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="Sources/面向对象的三大特性.html">
            
                
                    <a href="../Sources/面向对象的三大特性.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        面向对象的三大特性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
            <span><b>1.5.</b> 原型</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="Sources/传统构造函数存在的问题.html">
            
                
                    <a href="../Sources/传统构造函数存在的问题.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.1.</b>
                        
                        传统构造函数存在的问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="Sources/使用原型解决构造函数问题.html">
            
                
                    <a href="../Sources/使用原型解决构造函数问题.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.2.</b>
                        
                        使用原型解决构造函数问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="Sources/原型相关概念.html">
            
                
                    <a href="../Sources/原型相关概念.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.3.</b>
                        
                        原型相关概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="Sources/原型的使用.html">
            
                
                    <a href="../Sources/原型的使用.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.4.</b>
                        
                        原型的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="Sources/proto.html">
            
                
                    <a href="../Sources/proto.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.5.</b>
                        
                        \_\_proto\_\_
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="Sources/继承的实现方式.html">
            
                
                    <a href="../Sources/继承的实现方式.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        继承的实现方式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="Sources/原型三角绘制.html">
            
                
                    <a href="../Sources/原型三角绘制.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        原型三角绘制
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="Sources/属性搜索原则.html">
            
                
                    <a href="../Sources/属性搜索原则.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        属性搜索原则
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.9" >
            
            <span><b>1.9.</b> 原型链</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="Sources/原型链结构.html">
            
                
                    <a href="../Sources/原型链结构.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.9.1.</b>
                        
                        原型链结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="Sources/原型式继承.html">
            
                
                    <a href="../Sources/原型式继承.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.9.2.</b>
                        
                        原型式继承
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" >
            
            <span><b>1.10.</b> 对象相关的其他知识</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="Sources/objectprototype成员介绍.html">
            
                
                    <a href="../Sources/objectprototype成员介绍.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.10.1.</b>
                        
                        Object.prototype成员介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="Sources/function.html">
            
                
                    <a href="../Sources/function.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.10.2.</b>
                        
                        Function
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.10.2.1" data-path="Sources/function的使用.html">
            
                
                    <a href="../Sources/function的使用.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.10.2.1.</b>
                        
                        Function的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.10.2.2" data-path="Sources/参数代码太长的问题.html">
            
                
                    <a href="../Sources/参数代码太长的问题.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.10.2.2.</b>
                        
                        参数代码太长的问题
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="Sources/静态成员和实例成员.html">
            
                
                    <a href="../Sources/静态成员和实例成员.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.10.3.</b>
                        
                        静态成员和实例成员
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="Sources/arguments对象.html">
            
                
                    <a href="../Sources/arguments对象.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.10.4.</b>
                        
                        arguments对象
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >JavaScript高级讲义</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="function-&#x7684;&#x4F7F;&#x7528;">Function &#x7684;&#x4F7F;&#x7528;</h2>
<h3 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;:</h3>
<pre><code class="lang-js"><span class="hljs-comment">//Function&#x51FD;&#x6570;&#x6240;&#x6709;&#x7684;&#x53C2;&#x6570;&#x5168;&#x90FD;&#x662F;&#x5B57;&#x7B26;&#x4E32;</span>
<span class="hljs-comment">//Function&#x51FD;&#x6570;&#x7684;&#x4F5C;&#x7528;&#x5C31;&#x662F;&#x5C06;&#x6240;&#x6709;&#x7684;&#x53C2;&#x6570;&#x7EC4;&#x5408;&#x8D77;&#x6765;&#xFF0C;&#x53D8;&#x6210;&#x4E00;&#x4E2A;&#x51FD;&#x6570;</span>
<span class="hljs-comment">//1&#x3001;&#x5982;&#x679C;&#x53EA;&#x4F20;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5FC5;&#x7136;&#x662F;&#x51FD;&#x6570;&#x4F53;</span>
<span class="hljs-comment">//2&#x3001;&#x5982;&#x679C;&#x4F20;&#x591A;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x8868;&#x793A;&#x51FD;&#x6570;&#x4F53;&#xFF0C;&#x524D;&#x9762;&#x7684;&#x53C2;&#x6570;&#x4EE3;&#x8868;&#x5C06;&#x8981;&#x521B;&#x5EFA;&#x7684;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;</span>
<span class="hljs-comment">//3&#x3001;&#x5982;&#x679C;&#x4E0D;&#x4F20;&#x53C2;&#x6570;&#xFF0C;&#x8868;&#x793A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7A7A;&#x51FD;&#x6570;</span>
<span class="hljs-keyword">new</span> <span class="hljs-built_in">Function</span>(arg1, arg2, arg3, ..., argN, body);
</code></pre>
<h3 id="&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6253;&#x5370;&#x4E00;&#x53E5;&#x8BDD;&#x7684;&#x51FD;&#x6570;">&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6253;&#x5370;&#x4E00;&#x53E5;&#x8BDD;&#x7684;&#x51FD;&#x6570;</h3>
<pre><code class="lang-js"><span class="hljs-comment">//&#x4F20;&#x7EDF;&#x7684;&#x65B9;&#x5F0F;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x4F60;&#x597D;&quot;</span>);
}

<span class="hljs-comment">//&#x4F7F;&#x7528;Function</span>
<span class="hljs-keyword">var</span> func = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Function</span>(<span class="hljs-string">&quot;console.log(&apos;&#x4F60;&#x597D;&apos;);&quot;</span>);
</code></pre>
<p>&#x8FD9;&#x91CC;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x521B;&#x5EFA;&#x51FA;&#x6765;&#x7684;&#x51FD;&#x6570;&#x529F;&#x80FD;&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;</p>
<h3 id="&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7A7A;&#x51FD;&#x6570;">&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7A7A;&#x51FD;&#x6570;</h3>
<pre><code class="lang-js"><span class="hljs-comment">//&#x4F20;&#x7EDF;&#x7684;&#x65B9;&#x5F0F;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>)</span>{}

<span class="hljs-comment">//Function</span>
<span class="hljs-keyword">var</span> func = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Function</span>();
</code></pre>
<h3 id="&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6709;&#x53C2;&#x6570;&#x7684;&#x51FD;&#x6570;">&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6709;&#x53C2;&#x6570;&#x7684;&#x51FD;&#x6570;</h3>
<pre><code class="lang-js"><span class="hljs-comment">//&#x4F20;&#x7EDF;&#x7684;&#x65B9;&#x5F0F;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params">num</span>)</span>{
    <span class="hljs-built_in">console</span>.log(num);
}

<span class="hljs-comment">//Function</span>

<span class="hljs-keyword">var</span> func = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Function</span>(){<span class="hljs-string">&quot;num&quot;</span>, <span class="hljs-string">&quot;console.log(num);&quot;</span>};
</code></pre>
<h3 id="&#x7EC3;&#x4E60;-&#x5229;&#x7528;-function-&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x51FD;&#x6570;-&#x8981;&#x6C42;&#x4F20;&#x5165;&#x4E24;&#x4E2A;&#x6570;&#x5B57;-&#x6253;&#x5370;&#x5176;&#x548C;">&#x7EC3;&#x4E60;: &#x5229;&#x7528; Function &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x51FD;&#x6570;, &#x8981;&#x6C42;&#x4F20;&#x5165;&#x4E24;&#x4E2A;&#x6570;&#x5B57;, &#x6253;&#x5370;&#x5176;&#x548C;</h3>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> func = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Function</span>(
    <span class="hljs-string">&apos;num1&apos;</span>,
    <span class="hljs-string">&apos;num2&apos;</span>,
    <span class="hljs-string">&apos;console.log( num1 + num2 );&apos;</span>
);
</code></pre>
<h3 id="&#x7EC3;&#x4E60;-&#x5229;&#x7528;-function-&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x51FD;&#x6570;-&#x8981;&#x6C42;&#x5141;&#x8BB8;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x65F6;&#x4F20;&#x5165;&#x4EFB;&#x610F;&#x4E2A;&#x6570;&#x53C2;&#x6570;-&#x5E76;&#x4E14;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x8FD9;&#x4E9B;&#x6570;&#x5B57;&#x4E2D;&#x6700;&#x5927;&#x7684;&#x6570;&#x5B57;-&#x7EC3;&#x4E60;-&#x5229;&#x7528;-function-&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6C42;&#x4E09;&#x4E2A;&#x6570;&#x4E2D;&#x6700;&#x5927;&#x6570;&#x7684;&#x51FD;&#x6570;">&#x7EC3;&#x4E60;: &#x5229;&#x7528; Function &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x51FD;&#x6570;, &#x8981;&#x6C42;&#x5141;&#x8BB8;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x65F6;&#x4F20;&#x5165;&#x4EFB;&#x610F;&#x4E2A;&#x6570;&#x53C2;&#x6570;, &#x5E76;&#x4E14;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x8FD9;&#x4E9B;&#x6570;&#x5B57;&#x4E2D;&#x6700;&#x5927;&#x7684;&#x6570;&#x5B57;. &#x7EC3;&#x4E60;: &#x5229;&#x7528; Function &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6C42;&#x4E09;&#x4E2A;&#x6570;&#x4E2D;&#x6700;&#x5927;&#x6570;&#x7684;&#x51FD;&#x6570;.</h3>
<pre><code class="lang-js"><span class="hljs-comment">// &#x4F20;&#x7EDF;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span> (<span class="hljs-params"> a, b, c </span>)</span>{
    <span class="hljs-keyword">var</span> res = a &gt; b ? a : b;
    res = res &gt; c ? res : c;
    <span class="hljs-keyword">return</span> res;
}

<span class="hljs-comment">// Function</span>

<span class="hljs-keyword">var</span> func = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Function</span>( <span class="hljs-string">&apos;a&apos;</span>,
    <span class="hljs-string">&apos;b&apos;</span>,
    <span class="hljs-string">&apos;c&apos;</span>,
    <span class="hljs-string">&apos;var res = a &gt; b ? a : b;res = res &gt; c ? res : c;return res;&apos;</span> )
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../Sources/function.html" class="navigation navigation-prev " aria-label="Previous page: Function"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../Sources/参数代码太长的问题.html" class="navigation navigation-next " aria-label="Next page: 参数代码太长的问题"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
